package com.xinbochuang.machine.admin.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinbochuang.machine.admin.domain.GejieOrder;
import com.xinbochuang.machine.admin.domain.Macinout;
import com.xinbochuang.machine.admin.domain.ShigongOrder;
import com.xinbochuang.machine.admin.service.IMacinoutService;
import com.xinbochuang.machine.admin.service.IShigongOrderService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.domain.BaseEntity;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 杜文博
 * @date 2020-10-26
 */
@RestController
@RequestMapping("/shigong-order")
public class ShigongOrderController extends BaseController {


    @Autowired
    private IShigongOrderService shigongOrderService;
    @Autowired
    private IMacinoutService iMacinoutService;

    @GetMapping("/detail")
    public AjaxResult getShiGongDetail(@RequestParam("uuid") String uuid) {
        return AjaxResult.data(shigongOrderService.getShiGongDetail(uuid));
    }

    @GetMapping("/getAllShiGongOrder")
    public AjaxResult getAllShiGongOrder(@RequestParam("query") String query) {

        QueryWrapper<ShigongOrder> queryWrapper = new QueryWrapper<>();
        if(!query.equals("")&&!query.equals("undefined")){
            queryWrapper.lambda().like(ShigongOrder::getSgOrderName,query);
        }
        queryWrapper.lambda().eq(ShigongOrder::getDeleteFlag, 0);
        queryWrapper.lambda().like(ShigongOrder::getSgMacName,"银川市金凤区生产中心2号楼");
        queryWrapper.lambda().ge(ShigongOrder::getSgWorkEndTime, DateUtil.today());
        queryWrapper.notInSql("uuid","select order_id from macinout where order_id is not null group by order_id");
        queryWrapper.orderByAsc("sg_work_begin_time");
        return AjaxResult.data(shigongOrderService.list(queryWrapper));
    }

    /**
     * 查询分页列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(ShigongOrder order) {
        QueryWrapper<ShigongOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(BaseEntity::getCreateTime);
        if (StrUtil.isNotBlank(order.getSgOrderName())) {
            queryWrapper.lambda().like(ShigongOrder::getSgOrderName, order.getSgOrderName());
        }
        if (StrUtil.isNotBlank(order.getSgCreateOrderUserName())) {
            queryWrapper.lambda().like(ShigongOrder::getSgCreateOrderUserName, order.getSgCreateOrderUserName());
        }
        return getDataTable(shigongOrderService.page(new Page<>(order.getPageNum(), order.getPageSize()), queryWrapper));
    }
}

